package shuntianfu.com.shuntianfucompressor;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import business.MainManager;
import common.AppContext;
import common.Constants;
import model.Compressor;
import model.Compressors;
import model.StoreInfoModel;
import ui.LoadingProgressDialog;
import utils.CustomToast;
import utils.DataHandler;

public class CompressListActiviy extends AppCompatActivity implements View.OnClickListener {
	private static final String TAG = "CompressListActiviy";
	private ListView listView;

	private LinearLayout back;
	private LinearLayout setting;
	private LinearLayout refresh;

	private StoreInfoModel storeInfoModel;

	//    private LinkedList<Compressor> compressorList = new LinkedList<>();
	private List<Compressor> compressorList = new ArrayList<>();

	private MyAdapter adapter;
	private DataHandler compressorListDataHandler = new DataHandler() {
		@Override
		public void onData(int code, String msg, Object obj) {
			utils.Log.d(TAG, "compressorListDataHandler code=" + code);
			hideCustomProgressDialog();
			if (code == Constants.SERVER_SUCCESS) {

				Type listType = new TypeToken<List<Compressor>>() {
				}.getType();
				Gson gson = new Gson();
				if (compressorList.size() != 0) {
					compressorList.clear();
				}
				compressorList = gson.fromJson(obj.toString(), listType);
//                compressorList = gson.fromJson(obj.toString(),List.class);
//                compressorList = compressors.getCompressorList();
				Log.d(TAG, compressorList.toString());

				listView.setAdapter(adapter);

			} else {
				CustomToast.showToast(CompressListActiviy.this, "压缩机列表请求失败，刷新试试", 2000);
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_compress_list_activiy);
		showCustomProgrssDialog();

//        if (null != getIntent() && null != getIntent().getExtras()) {
//            storeInfoModel = (StoreInfoModel) getIntent().getExtras().getSerializable("STOREINFO");
//        }
		if (AppContext.getInstance().getStoreInfoModel() != null) {
			storeInfoModel = AppContext.getInstance().getStoreInfoModel();
		}

		initView();
		initData();
	}

	private void initData() {
		adapter = new MyAdapter();
		getYSJList();
	}

	private void getYSJList() {

		if (null != storeInfoModel) {
			utils.Log.d(TAG, "storeInfoModel.getId()=" + storeInfoModel.getId());
			MainManager.getInstance().getCompressorList(this, storeInfoModel.getId(), compressorListDataHandler);
		} else {
			CustomToast.showToast(this, "超市的ID为空，点击重试", 2000);
			finish();
		}

	}

	private void initView() {
		back = (LinearLayout) findViewById(R.id.title_back);
		listView = (ListView) findViewById(R.id.lv_compress);
		listView.setOnItemClickListener(itemClickListener);
		
		refresh = (LinearLayout) findViewById(R.id.title_refresh);
		setting = (LinearLayout) findViewById(R.id.title_setting);

		back.setOnClickListener(this);
		refresh.setOnClickListener(this);
		setting.setOnClickListener(this);
	}

	AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			if (compressorList.size() <= 0||position>=compressorList.size()) {

				return;
			}
			Intent intent = new Intent(CompressListActiviy.this, SettingFazhiActivity.class);
			Bundle bundle = new Bundle();
			bundle.putParcelable("COMPRESSOR", compressorList.get(position));
			intent.putExtras(bundle);
			CompressListActiviy.this.startActivity(intent);

			CompressListActiviy.this.overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_back:
				back();
				break;
			case R.id.title_refresh:
				refresh();
				break;
			case R.id.title_setting:
				gotoSetting();
				break;
			default:
				break;
		}
	}

	private void gotoSetting() {
		Intent intent = new Intent(this, SettingActivity.class);
		startActivity(intent);
		CompressListActiviy.this.overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
	}

	private void refresh() {
		showCustomProgrssDialog();
		getYSJList();
	}

	private void back() {
		finish();
		overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
	}


	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return compressorList.size();
		}

		@Override
		public Object getItem(int position) {
			return compressorList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {

			MyViewHolder holder = null;
			if (null == convertView) {
				holder = new MyViewHolder();

				convertView = LayoutInflater.from(CompressListActiviy.this).inflate(R.layout.item_compress_list, null);

				holder.tvName = (TextView) convertView.findViewById(R.id.tv_compress_name);
				holder.ivEdit = (LinearLayout) convertView.findViewById(R.id.ll_fazhi_edit);
				holder.ivLook = (LinearLayout) convertView.findViewById(R.id.ll_look_for);
				convertView.setTag(holder);

			} else {
				holder = (MyViewHolder) convertView.getTag();
			}

			holder.ivEdit.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {

					Intent intent = new Intent(CompressListActiviy.this, SettingFazhiActivity.class);
					Bundle bundle = new Bundle();
					bundle.putParcelable("COMPRESSOR", compressorList.get(position));
					intent.putExtras(bundle);
					CompressListActiviy.this.startActivity(intent);

					CompressListActiviy.this.overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
//                    CustomToast.showToast(CompressListActiviy.this, "dianji=" + compressorList.get(position).getCompressorName(), 2000);
				}
			});
			holder.ivLook.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {

					Intent intent = new Intent(CompressListActiviy.this, MainActivity.class);
					intent.putExtra("COMPRESSORS", (Serializable) compressorList);
					Bundle bundle = new Bundle();
					bundle.putInt("POSITION", position);

					intent.putExtras(bundle);
					CompressListActiviy.this.startActivity(intent);
					CompressListActiviy.this.overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
//                    CustomToast.showToast(CompressListActiviy.this, "dianji=" + compressorList.get(position).getCompressorName(), 2000);
				}
			});

			Compressor compressor = compressorList.get(position);

			if (null != compressor.getCompressorName()) {
				holder.tvName.setText(compressor.getCompressorName());
			}

			return convertView;
		}
	}


	class MyViewHolder {

		TextView tvName;
		LinearLayout ivEdit;
		LinearLayout ivLook;

	}


	protected LoadingProgressDialog loadingProgressDialog;

	public void showCustomProgrssDialog() {

		if (null == loadingProgressDialog)

			loadingProgressDialog = LoadingProgressDialog.createDialog(this);

		loadingProgressDialog.setCanceledOnTouchOutside(false);//点击无效，回退有效

//        loadingProgressDialog.setCancelable(false);//点击无效，回退无效

	}

	public void hideCustomProgressDialog() {

		if (null != loadingProgressDialog) {

			loadingProgressDialog.dismiss();

			loadingProgressDialog = null;

		}

	}


}
